package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.SkuBasicData;
import com.ygqh.baby.model.SkuCombinationInfo;
import com.ygqh.baby.model.SkuCombinationQuery;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.YgSkuCombination;
import com.ygqh.baby.service.YgSkuCombinationService;
import com.ygqh.baby.service.YgSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 */
@Controller
@RequestMapping("/admin/skuCombination")
public class SkuCombinationController extends BaseController<Object, Long> {

    @Autowired
    private YgSkuCombinationService ygSkuCombinationService;
    @Autowired
    private YgSkuService ygSkuService;


    @RequestMapping("search")
    @ResponseBody
    public Map<String, Object> searchSupplier(SkuCombinationQuery query) {
        ResultSet<YgSkuCombination> search = ygSkuCombinationService.search(query);
        return search.toDatatables(null);
    }

    @RequestMapping("edit")
    @ResponseBody
    public ResponseEntity edit(@RequestBody List<YgSkuCombination> skuCombination) {
        AdminUser session = getSession();
        Date now = new Date();
        int i = 0;
        for (YgSkuCombination ygSkuCombination : skuCombination) {
            ygSkuCombination.setCreateTime(now);
            ygSkuCombination.setCreateBy(session.getTrueName());
            i += ygSkuCombinationService.saveOrUpdate(ygSkuCombination);
        }
        return ResponseEntity.success(i);
    }

    @RequestMapping("delete")
    @ResponseBody
    public ResponseEntity edit(@RequestParam Long id) {
        int i = ygSkuCombinationService.deleteSoft(id);
        return ResponseEntity.success(i);
    }

    @RequestMapping("findSkuCombinationInfo")
    @ResponseBody
    public ResponseEntity findSkuCombinationInfo(@RequestParam Long skuId) {
        List<YgSkuCombination> list = ygSkuCombinationService.findListBySkuIds(skuId);
        SkuBasicData sku = ygSkuService.getBasicDataBySkuId(skuId);
        return ResponseEntity.success(new SkuCombinationInfo(sku, list));
    }


}
